package client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class JournalReader extends JFrame {
	private String currentJournalName;
	
	private JTextArea textArea;
	private JButton saveButton;
	
	ClientSocketConnection connection;
	
	public JournalReader(ClientSocketConnection clientSocketConnection) {
		connection = clientSocketConnection;
		
		setTitle("Journal Reader");
		
		textArea = new JTextArea(20, 50);
		saveButton = new JButton("SAVE TO SERVER");
		
		ActionListener buttonListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				connection.requestWriteJournal(currentJournalName, textArea.getText());
			}
		};
		
		saveButton.addActionListener(buttonListener);
		
		JPanel panel = new JPanel();
		panel.add(textArea, BorderLayout.NORTH);
		panel.add(saveButton, BorderLayout.SOUTH);
		
		add(panel);
		pack();
	}

	public void openJournal(String journalName, String journal) {
		currentJournalName = journalName;
		textArea.setText(journal);
		setVisible(true);
	}
}
